package com.ktjy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ktjy.pojo.Product;
import com.ktjy.service.IProductService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jjx
 * @since 2025-01-16
 */
@Controller
@RequestMapping("/product")
public class ProductController {
    Logger logger = LoggerFactory.getLogger(ProductController.class);
    @Autowired
    IProductService iProductService;

    @RequestMapping("/list")
    public String list(Product product,
                       @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
                       @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                       Model model) {
        try {
            //组装分页条件
            Page<Product> page=new Page<>(pageIndex,pageSize);
            //组装过滤条件 动态sql
            QueryWrapper<Product> qw=new QueryWrapper();
            if (product.getProdName() != null && !product.getProdName().equals("")) {
                qw.like("prod_name",product.getProdName());
            }
            if (product.getProdType() != null && !product.getProdType().equals("")) {
                qw.like("prod_type",product.getProdType());
            }
            if (product.getProdBatch() != null && !product.getProdBatch().equals("")) {
                qw.like("prod_batch",product.getProdBatch());
            }
            //调用分页方法
            IPage<Product> pager=iProductService.page(page, qw);
            model.addAttribute("pager", pager);

            //回显数据
            model.addAttribute("condition", product);
        } catch (Exception e) {
            logger.error("ProductController 中的 list 方法执行发生错误。 ", e);
            throw new RuntimeException(e);
        }
        return "product/list";
    }
}
